package cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.start;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.InstanceContextDataRequest;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.Validate;
import org.junit.Assert;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;


public class SubmitAnalyzeWorkReportFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setWorkTaskResponse(flowContext);
        completeTask(flowContext);
        checkContextData(flowContext);
    }

    @Override
    public String name() {
        return "提交分析工作报告";
    }

    private void setWorkTaskResponse(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList = Arrays.asList(workFlowInstance.getId());
        workFlowTaskQuery.initiatorList = Arrays.asList(startWorkFlowRequest.initiator);
        workFlowTaskQuery.instanceStatusList = Arrays.asList(WorkFlowInstanceStatus.Running.status);

        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        Validate.notNull(workFlowTaskResponse, "工作流任务响应不能为空");
        flowContext.putInstanceData(workFlowTaskResponse);
    }

    private void completeTask(FlowContext flowContext){
        WorkFlowTaskResponse workFlowTaskResponse = flowContext.checkInstanceData(WorkFlowTaskResponse.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        Assert.assertTrue(workFlowTaskResponse.contextData.containsKey("firstAuditUserId"));
        Assert.assertTrue(workFlowTaskResponse.contextData.containsKey("secondAuditUserId"));
        workFlowTaskResponse.contextData.put("submitTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        CompleteTaskRequest completeTaskRequest = new CompleteTaskRequest();
        completeTaskRequest.taskId = workFlowTaskResponse.taskId;
        completeTaskRequest.taskType = TaskType.COMPLETE;
        completeTaskRequest.contextData.putAll(workFlowTaskResponse.contextData);
        quickWorkFlow.completeTask(completeTaskRequest);
    }

    private void checkContextData(FlowContext flowContext){
        WorkFlowTaskResponse workFlowTaskResponse = flowContext.checkInstanceData(WorkFlowTaskResponse.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        InstanceContextDataRequest contextDataRequest = new InstanceContextDataRequest();
        contextDataRequest.workFlowTaskId = workFlowTaskResponse.taskId;
        JSONObject contextData = quickWorkFlow.getWorkFlowInstanceContextData(contextDataRequest);
        Assert.assertTrue(contextData.containsKey("submitTime"));
    }
}
